package com.faxsun.core.persist.dao.impl;

import java.util.ArrayList;
import java.util.List;

import mtons.modules.persist.impl.DaoImpl;

import org.hibernate.Query;

import com.faxsun.core.persist.dao.LiveManagerDao;
import com.faxsun.core.persist.entity.LiveManagerPO;
import com.faxsun.core.persist.entity.LivePO;

public class LiveManagerDaoImpl extends DaoImpl<LiveManagerPO> implements
		LiveManagerDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7110920294313149666L;

	protected LiveManagerDaoImpl() {
		super(LiveManagerPO.class);
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<LiveManagerPO> findAllManagers(long liveId) {
		// TODO Auto-generated method stub
		return super.findBy("liveId", liveId);
	}

	@Override
	public LiveManagerPO find(long liveId, long userId) {
		return super.findUnique(
				"from LiveManagerPO where userId=? and liveId=?", userId,
				liveId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LivePO> find(long userId, String role) {
		Query q = super.createQuery(
				"from LiveManagerPO where  userId=? and role=?", userId, role);
		List<?> list = q.list();
		if (list.size() == 0) {
			return new ArrayList<LivePO>();
		}
		List<Long> ids = new ArrayList<Long>();
		for (Object o : list) {
			ids.add(((LiveManagerPO) o).getLiveId());
		}
		String hql = "from LivePO where id in (:liveIds)";
		Query query = super.createQuery(hql);
		query.setParameterList("liveIds", ids);
		return query.list();
	}

	@Override
	public LiveManagerPO getLiveAdmin(long liveId) {
		return super.findUnique(
				"from LiveManagerPO where role='ADMIN' and liveId=?", liveId);
	}

	}
